package com.hustmed.controller;

import java.lang.reflect.Array;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.hustmed.domain.ProcessParams;
import com.hustmed.domain.Sample;
import com.hustmed.service.IProcessParamsService;
import com.hustmed.service.ISampleService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.hustmed.domain.Experiment;
import com.hustmed.service.IExperimentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实验Controller
 *
 */
@RestController
@RequestMapping("/hustmed/experiment")
public class ExperimentController extends BaseController
{
    @Autowired
    private IExperimentService experimentService;

    /**
     * 查询实验列表
     */
    @PreAuthorize("@ss.hasPermi('hustmed:experiment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Experiment experiment)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
//        startPage();
        List<Experiment> list = experimentService.selectExperimentList(experiment);
        int num = list.size();
        list = list.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(num);
        return rspData;

//        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('hustmed:sample:list')")
    @GetMapping("/sampleList")
    public TableDataInfo sampleList(Experiment experiment)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<Experiment> list = experimentService.sampleList(experiment);
        int num = list.size();
        list = list.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(num);
        return rspData;
    }

    /**
     * 导出实验列表
     */
    @PreAuthorize("@ss.hasPermi('hustmed:experiment:export')")
    @Log(title = "实验", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Experiment experiment)
    {
        List<Experiment> list = experimentService.selectExperimentList(experiment);
        for(Experiment e:list ){
            String epersons = "";
            for(SysUser sysUser:e.getSysUsers()){
                epersons=epersons+sysUser.getUserName()+",";
            }
            e.setExperimentPersons(epersons);
        }
        ExcelUtil<Experiment> util = new ExcelUtil<Experiment>(Experiment.class);
        if(experiment.getEtype().equals("2")){
            String[] myArray = {"status"};
            util.excludeFields(myArray);
        }
        util.exportExcel(response, list, "实验数据");
    }

    /**
     * 获取实验详细信息
     */
    @PreAuthorize("@ss.hasPermi('hustmed:experiment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        Experiment experiment = new Experiment();
        experiment.setId(id);
        List<Experiment> elist = experimentService.selectExperimentList(experiment);
        experiment = elist.get(0);
        List<ProcessParams> pList = experiment.getProcessParamsList();
        for(ProcessParams i : pList){

            Collections.sort(i.getSampleList(), new Comparator<Sample>() {
                @Override
                public int compare(Sample p1, Sample p2) {
                    return p1.getSampleNo().compareTo(p2.getSampleNo());
                }
            });
        }
        Collections.sort(pList, new Comparator<ProcessParams>() {
            @Override
            public int compare(ProcessParams p1, ProcessParams p2) {
                return p1.getProcessParamsNo().compareTo(p2.getProcessParamsNo());
            }
        });
        return success(experiment);
    }

    /**
     * 新增实验
     */
    @PreAuthorize("@ss.hasPermi('hustmed:experiment:add')")
    @Log(title = "实验", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Experiment experiment)
    {
        return toAjax(experimentService.insertExperiment(experiment));
    }

    /**
     * 修改实验
     */
    @PreAuthorize("@ss.hasPermi('hustmed:experiment:edit')")
    @Log(title = "实验", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Experiment experiment)
    {
        return toAjax(experimentService.updateExperiment(experiment));
    }

    /**
     * 删除实验
     */
    @PreAuthorize("@ss.hasPermi('hustmed:experiment:remove')")
    @Log(title = "实验", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(experimentService.deleteExperimentByIds(ids));
    }
}
